package _base0to1;

/**
 * 50. Pow(x, n)
 */
public class No50 {
    public double myPow(double x, int n) {
        long p = n;
        p = Math.abs(p);

        double temp = x;
        double answer = 1;
        while (p > 0) {
            if ((p & 1) == 1) {
                p--;
                answer *= temp;
            } else {
                p = p >>> 1;
                temp *= temp;
            }
        }
        return n > 0 ? answer : 1 / answer;
    }
}
